<?php

namespace Common\Model;
use think\Model;

class UserKuerpWarehouseInvModel extends UserKuerpWarehouseModel{

	//追加属性
    public $append = [
        'type_name',
		'wh_name',
        'loc_name',
		'product',
        'nums',
        'inv_nums',
        'show_nums',
	];

    /**
     * 当时库存数量
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getInvNumsAttr($value, $data){
		return floatval($data['inv_nums']);
    }

    /**
     * 盘点数量
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getNumsAttr($value, $data){
		return floatval($data['nums']);
    }

    /**
     * 误差数量
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getShowNumsAttr($value, $data){
        return abs($data['nums']-$data['inv_nums']);
    }

    /**
     * 类型
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getTypeNameAttr($value, $data){
		$status_text = '--';
		if($data['type']==1){
			$status_text = '盘盈（多出）';
		}else if($data['type']==2){
			$status_text = '盘亏（少了）';
		}else if($data['type']==3){
			$status_text = '持平';
		}
		return $status_text;
    }

    /**
     * 商品信息
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getProductAttr($value, &$data){
        if($data['item_id']){
            $model = D('DesigenGuigedetail');
            $wdata['id'] = $data['item_id'];
            $vo = $model->where($wdata)->find();
            $product_name = $vo['source']['title'].'-<font color="red">'.$vo['guige'].' '.$vo['color'].'</font>';
            $vo['item_id'] = $vo['id'];
            $stock = $model->getStock($data['item_id']);
            //dump($vo);exit;
        }else{
            if($data['atype']==1){
                $model = D('DesigenList');
                $wdata['id'] = $data['product_id'];
                $vo = $model->where($wdata)->find();
                $product_name = $vo['title'];
                //$stock = $model->getNumberAttr($data['product_id']);
            }else if($data['atype']==2){
                $model = D('Img');
                $wdata['id'] = $data['product_id'];
                $vo = $model->where($wdata)->find();
                $product_name = $vo['title'];
                //$stock = $model->getNumberAttr($data['product_id']);
            }else if($data['atype']==10){
                $model = D('KuerpOtherProduct');
                $wdata['id'] = $data['product_id'];
                $vo = $model->where($wdata)->find();
                $product_name = $vo['title'];
                //$stock = $model->getNumberAttr($data['product_id']);
            }
            $vo['item_id'] = 0;
            $stock_ar['id'] = $data['product_id'];
            $stock = $model->getNumberAttr('',$stock_ar);
        }
        if(MODULE_NAME=='Api'){
            $product_name = strip_tags($product_name);
        }
        $data['product_name'] = $product_name;
        $data['stock'] = $stock;
        return $vo;
    }

    //添加数据
    function addData($request){
        $return = [
            'status' => false,
            'errcode'   => 10000,
            'data'   => '',
            'msg'    => '失败'
        ];
        $sqls = array();
        $data = $request;
        if($request['nums']<0){
            $return['msg'] = '数量不能低于0,必须是正整数 - 1000';
            return $return;
        }
        if(!$request['wh_id']){
            $return['msg'] = '仓库必须 - 1000-2';
            return $return;
        }
        if(!$request['loc_id']){
            $return['msg'] = '库位必须 - 1000-3';
            return $return;
        }
        if($request['item_id']){
            //有规格商品
            $p_model = D('DesigenGuigedetail');
            $p_wdata['id'] = $request['item_id'];
            $p_vo = $p_model->where($p_wdata)->find();
            //查货号
            if($p_vo['atype']==1){
                $h_model = D('DesigenList');
            }else{
                $h_model = D('Img');
            }
            $h_wdata['id'] = $p_vo['design_id'];
            $h_vo = $h_model->where($h_wdata)->find();
            $p_vo['goods_sn'] = $h_vo['goods_sn'];
        }else{
            if($request['atype']==1){
                //成品
                $p_model = D('DesigenList');
                $p_wdata['id'] = $request['design_id'];
                $p_vo = $p_model->where($p_wdata)->find();
                //dump($request);echo M()->_sql();exit;
            }else if($request['atype']==2){
                //辅料
                $p_model = D('Img');
                $p_wdata['id'] = $request['design_id'];
                $p_vo = $p_model->where($p_wdata)->find();
            }else if($request['atype']==10){
                //其他商品
                $p_model = D('KuerpOtherProduct');
                $p_wdata['id'] = $request['design_id'];
                $p_vo = $p_model->where($p_wdata)->find();
            }else{
                $return['msg'] = '提交信息有误 - 1000-4';
                return $return;
            }
            $stock_ar['id'] = $data['product_id'];
            $stock = $p_model->getNumberAttr('',$stock_ar);
        }
        if(!$p_vo){
            $return['msg'] = '商品不存在 - 1000-5';
            return $return;
        }
        $kw_model = D('UserKuerpWarehouse');
        $input['atype'] = $request['atype'];
        $input['product_id'] = $request['product_id'];
        $input['item_id'] = $request['item_id'];
        $input['loc_id'] = $request['loc_id'];
        $stock = $kw_model->getProductLocStock($input);
        $goods_sn = $p_vo['goods_sn'];//货号
        //dump($request);dump($p_vo);exit;
        if($request['nums']>$stock){
            $type = 1;//需要入库
        }else if($request['nums']==$stock){
            $type = 3;
        }else{
            $type = 2;//需要出库
        }
        //增加历史数据
        $data['inv_nums'] = $stock;
        $data['atype'] = $request['atype'];
        $data['product_id'] = $request['product_id'];
        $data['item_id'] = $request['item_id'];
        $data['goods_sn'] = $goods_sn;
        $data['type'] = $type;
        $data['add_time'] = NOW_TIME;
        $res = $this->add($data);
        if(!$res){
            $return['msg'] = '历史记录添加出错 - 1005';
            return $return;
        }
        $sqls[] = M()->_sql();
        //dump($sqls);exit;
        //$return['data'] = $return_data;
        $return['sqls'] = $sqls;
        $return['status'] = true;
        $return['msg'] = '盘点完成';
        return $return;
    }


}